﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;

namespace housingsafety.server.Caching.Core
{
    public class DistributedCacheProvider : ICacheProvider
    {
        private static readonly string _LoggerCategoryName = "Cache";
        private readonly ILogger m_Logger;
        private readonly IMemoryCache m_MemoryCache;
        private readonly IDistributedCache m_DistributedCache;
        private readonly ICacheSerializer m_CacheSerializer;

        public string LoggerCategoryName { get; set; } = _LoggerCategoryName;

        public DistributedCacheProvider(ILoggerFactory loggerFactory, IMemoryCache memoryCache, IDistributedCache distributedCache, ICacheSerializer cacheSerializer)
        {
            m_MemoryCache = memoryCache;
            m_Logger = loggerFactory.CreateLogger(LoggerCategoryName);
            m_DistributedCache = distributedCache;
            m_CacheSerializer = cacheSerializer;
        }
        public ICache CreateCache()
        {
            return CreateCache(Helpers.ConstRedisKeys.DefaultNamespace);
        }
        public ICache CreateCache(string name)
        {
            return new DistributedCacheFacade(m_MemoryCache, m_DistributedCache, m_CacheSerializer, m_Logger, name);
        }
    }
}
